# RESTful DDS is a prototyping effort to demonstrate how DDS could be combined
# with HTTP clients, extending the realm of pub/sub distribution to true wide
# area networking.
#
# Copyright (C) 2009 PrismTech Ltd.
# reinier.torenbeek@gmail.com
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License Version 3 dated 29 June 2007, as published by the
# Free Software Foundation.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with DDSTouchStone; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

# First test OSPL_HOME env var, required for this script to run properly
if [ -z "$OSPL_HOME" ]
then
  echo OpenSplice environment does not appear to be set up -- exiting
  exit
fi

GEN_DIR=generated
OBJ_DIR=obj
LIB_DIR=lib

SERVICE_PKG=org.opensplice.restful.service
SERVICE_DIR=org/opensplice/restful/service
SERVICE_SOURCE_DIR=src/$SERVICE_DIR
SERVICE_JAR=$LIB_DIR/$SERVICE_PKG.jar

CHAT_DIR=Chat
CHAT_SOURCE_DIR=$GEN_DIR/$CHAT_DIR

# Source shell script containing locations of external jar files.
# Edit externals.sh if you want to point to the jars other than the
# default locations.
source ./externals.sh
EXTERNAL_JARS=$RESTLET_INTF_JAR:$RESTLET_IMPL_JAR:$GSON_JAR

# First generate code for built in test
# This is not required for the service to work, but is left in here for
# testing purposes.
echo Generating test classes using OpenSplice
mkdir -p $GEN_DIR
idlpp -S -l java -d $GEN_DIR $CHAT_IDL

# Compile generated source code
echo Compiling generated Tutorial source code
mkdir -p $OBJ_DIR
javac -sourcepath $GEN_DIR -d $OBJ_DIR -classpath "$OBJ_DIR:$DDS_JAR" $CHAT_SOURCE_DIR/*.java

# Compile service source code
echo Compiling RESTfulDDS service source code
mkdir -p $OBJ_DIR
javac -sourcepath $SERVICE_SOURCE_DIR -d $OBJ_DIR -classpath "$OBJ_DIR:$DDS_JAR:$EXTERNAL_JARS" $SERVICE_SOURCE_DIR/*.java

# Create jar
echo Creating RESTfulDDS service jar file, including the Chat files
mkdir -p $LIB_DIR
jar cf $SERVICE_JAR -C $OBJ_DIR $SERVICE_DIR  -C $OBJ_DIR $CHAT_DIR

# Prepare to build java chatroom example
if [ -e Tutorial ]
then
    echo Skipped building of chatroom tutorial because it seems to be there already
else
    echo Copying and building OpenSplice chatroom tutorial
    cp -r $OSPL_HOME/examples/dcps/standalone/Java/Tutorial .
    (cd Tutorial
    sh BUILD)
fi
